import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
import Http from "@/service/http";
const getDefaultState = () => {
    return {
        token: getToken(),
        userInfo: {},
        name: '',
        avatar: ''
    }
}

const state = getDefaultState()

const mutations = {
    RESET_STATE: (state) => {
        Object.assign(state, getDefaultState())
    },
    SET_TOKEN: (state, token) => {
        state.token = token
    },
    SET_NAME: (state, name) => {
        state.name = name
    },
    SET_AVATAR: (state, avatar) => {
        state.avatar = avatar
    },
    setUserInfo(state, data) {
        console.log(data, "用户信息")
        state.userInfo = data;
    }
}

const actions = {
    async getAdminUser() {
        let { code, data } = await Http.Http.getMenu();
        if (code && data?.menu) {
            // id为1是总和权限
            let role = data.menu.find(v => v.id == 35);
            if (role) {
                localStorage.setItem("menuInfo", JSON.stringify(role.child))
                return true;
            }
        }
        return false;
    },

    // remove token
    resetToken({ commit }) {
        return new Promise(resolve => {
            removeToken() // must remove  token  first
            commit('RESET_STATE')
            resolve()
        })
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}

